package cn.sinata.wanglin.neighbor.activity;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.NewFriendsAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.PushDao;
import cn.sinata.wanglin.model.Contact;
import cn.sinata.wanglin.model.NewFriend;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class NewFriendsActivity extends RecyclerListActivity {

    private NewFriendsAdapter newFriendsAdapter;
    private ArrayList<NewFriend> friends = new ArrayList<>();
    private int pageNo = 0;
    private String userId;
    private String account;

    @Override
    protected void initView() {
        super.initView();
        setTitle("新的好友");
        addRightButton("添加好友", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext)
                        .go(AddContactActivity.class)
                        .start();
            }
        });

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        account = SharedPreferencesUtils.getString(Constant.SP_IM_ACCOUNT);
        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        newFriendsAdapter = new NewFriendsAdapter(friends);
        View headerView = LayoutInflater.from(this).inflate(R.layout.header_new_friends_layout,null);
        View tv_contacts_friends = headerView.findViewById(R.id.tv_contacts_friends);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headerView.setLayoutParams(params);
        newFriendsAdapter.setHeaderView(headerView);

        tv_contacts_friends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext)
                        .go(ContactsFriendsActivity.class)
                        .start();
            }
        });

        return newFriendsAdapter;
    }

    void getData() {

//        for (int i = 0; i < 10; i++) {
//            NewFriend newFriend = new NewFriend();
//            friends.add(newFriend);
//        }
//        newFriendsAdapter.notifyDataSetChanged();
        HttpManager.getNewFriends(userId,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<NewFriend>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<NewFriend> newFriends) {

                        PushDao.getInstance(mContext).setNewFriendMessageAllRead(account);
                        if (pageNo == 0) {
                            friends.clear();
                        }
                        if (newFriends.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("没有数据");
                            } else {
                                pageNo -- ;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        friends.addAll(newFriends);
                        newFriendsAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }
}
